<template>
  <view class="contentview">
    <view class="myview">
      {{ info }}
    </view>

    <button type="primary" class="button" @tap="lanyatest1">1初始化蓝牙</button>
    <button type="primary" class="button" @tap="lanyatest2">
      2获取蓝牙状态
    </button>
    <button type="primary" class="button" @tap="lanyatest3">
      3搜索周边设备
    </button>
    <button type="primary" class="button" @tap="lanyatest4">
      4获取所有设备
    </button>
    <view v-for="(item, index) in devices" :key="index">
      <button
        type="primary"
        class="button"
        :id="item.deviceId"
        style="background-color: red"
        @tap="lanyaconnect"
      >
        5连接{{ item.name }}
      </button>
    </view>
  </view>
</template>

<script>
export default {
  name: "",
  data() {
    return {
      info: "未初始化蓝牙适配器",
      connectedDeviceId: "",
      devices: [],
    };
  },
  methods: {
    lanyatest1(event) {
      var that = this;
      wx.openBluetoothAdapter({
        success: function (res) {
          console.log("初始化蓝牙适配器成功");
          //页面日志显示
          that.info = "初始化蓝牙适配器成功";
        },
        fail: function (res) {
          console.log("请打开蓝牙和定位功能");
          that.info = "请打开蓝牙和定位功能";
        },
      });
    },

    lanyatest2(event) {
      var that = this;
      wx.getBluetoothAdapterState({
        success: function (res) {
          //打印相关信息
          console.log(
            JSON.stringify(res.errMsg) + "\n蓝牙是否可用：" + res.available
          );

          that.info =
            JSON.stringify(res.errMsg) + "\n蓝牙是否可用：" + res.available;
        },
        fail: function (res) {
          //打印相关信息
          console.log(
            JSON.stringify(res.errMsg) + "\n蓝牙是否可用：" + res.available
          );

          that.info =
            JSON.stringify(res.errMsg) + "\n蓝牙是否可用：" + res.available;
        },
      });
    },

    lanyatest3(event) {
      var that = this;
      wx.startBluetoothDevicesDiscovery({
        // services: ["FEE7"], //如果填写了此UUID，那么只会搜索出含有这个UUID的设备，建议一开始先不填写或者注释掉这一句
        success: function (res) {
          that.info = "搜索设备" + JSON.stringify(res);
          console.log("搜索设备返回" + JSON.stringify(res));
        },
        fail: (err) => {
          console.log(err);
        },
      });
    },

    lanyatest4(event) {
      var that = this;
      wx.getBluetoothDevices({
        success: function (res) {
          that.info = "设备列表\n" + JSON.stringify(res.devices);
          that.devices = res.devices;
          console.log("搜设备数目：" + res.devices.length);
          console.log("设备信息：\n" + JSON.stringify(res.devices) + "\n");
        },
        fail: (err) => {
          console.log(err);
        },
      });
    },

    lanyaconnect(event) {
      var that = this;
      wx.createBLEConnection({
        deviceId: event.currentTarget.id,
        success: function (res) {
          console.log("调试信息：" + res.errMsg);
          that.connectedDeviceId = event.currentTarget.id;
          that.info =
            "MAC地址：" + event.currentTarget.id + "  调试信息：" + res.errMsg;
        },
        fail: function (err) {
          console.log(err);
          console.log("连接失败");
        },
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.vertical {
  display: flex;
  flex-direction: column;
}

/**index.wxss**/
.horizontal {
  display: flex;
  flex-direction: row;
}

.btinfo {
  height: 100px;
}

.contentview {
  margin: 0 10px;
}

.button {
  margin: 5px;
}

.myview {
  height: 200px;
  word-break: break-all; /* 自动换行 */
}
</style>